package com.example.rg.generator.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.rg.generator.commons.R;
import com.example.rg.generator.domain.Employee;
import com.example.rg.generator.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestControllerAdvice
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeServices;

    /**
     * 员工登录
     *
     * @param servletRequest
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest servletRequest, @RequestBody Employee employee) {
        //获取密码
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //对比数据库用户数据
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, employee.getUsername());
        Employee one = employeeServices.getOne(lqw);
        //如果没有查询到则返回登录失败结果
        if (one == null) {
            return R.error("登录失败");
        }
        //密码比对，如果不一致则返回登录失败结果
        if (!one.getPassword().equals(password)) {
            return R.error("密码错误");
        }
        //查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if (one.getStatus() == 0) {
            return R.error("用户已禁用");
        }
        //登录成功，将员工id存入Session并返回登录成功结果
        servletRequest.getSession().setAttribute("employee", one.getId());
        return R.success(one);
    }

    //员工注销
    @PostMapping("/logout")
    private R<String> logout(HttpServletRequest servletRequest) {
        //清除Session中的数据
        servletRequest.getSession().removeAttribute("employee");
        return R.success("用户注销成功");
    }

    //增加
    @PostMapping
    private R<String> save(HttpServletRequest servletRequest, @RequestBody Employee employee) {
        //获取用户id
        Object userid = servletRequest.getSession().getAttribute("employee");

        //添加创建用户与修改用户
        //已设置自动填充
//        employee.setCreateUser((Long) userid);
//        employee.setUpdateUser((Long) userid);

        //设置用户初始密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        //添加修改时间与修改时间
        //已设置自动填充
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());

        employeeServices.save(employee);
        return R.success("添加成功");
    }

    //分页
    @GetMapping("/page")
    private R<Page<Employee>> page(int page, int pageSize, String name) {
        //建造分页构造器
        Page<Employee> employeePage = new Page<>(page, pageSize);
        //选择构造器
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //过滤条件
        lqw.like(Strings.isNotEmpty(name), Employee::getName, name);
        //排序条件
        lqw.orderByAsc(Employee::getId).orderByDesc(Employee::getUpdateTime);

        employeeServices.page(employeePage, lqw);

        return R.success(employeePage);
    }

    //修改
    @PutMapping
    private R<String> update(HttpServletRequest servletRequest, @RequestBody Employee employee) {
        //获取用户id
//        Object attribute = servletRequest.getSession().getAttribute("employee");
        //添加修改时间与修改用户
        //已设置自动填充
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser((Long) attribute);

        employeeServices.updateById(employee);
        return R.success("员工消息修改成功");
    }

    //回显
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id) {
        Employee byId = employeeServices.getById(id);
        if (byId != null) {
            return R.success(byId);
        }
        return R.error("无员工信息");
    }

}
